
import { getNotice, checkNotice, deletedNotice, getNoticeByCreateUser } from '@/services/notice'

export default {

    namespace: 'notice',

    state: {
        noticeList: [],
        noticeListByCreateUser: []
    },

    effects: {

        *getNotice({ userId }, { call, put }) {
            const result = yield call(getNotice, { user_id: userId })
            yield put({
                type: 'beforeChangeState',
                result,
                field: 'noticeList'
            })
            return result
        },

        *getNoticeByCreateUser({ userId }, { call, put }) {
            const result = yield call(getNoticeByCreateUser, { user_id: userId })
            yield put({
                type: 'beforeChangeState',
                result,
                field: 'noticeListByCreateUser'
            })
            return result
        },

        *checkNotice({ noticeId }, { call, put }) {
            const result = yield call(checkNotice, { notice_id: noticeId })
            yield put({
                type: 'beforeChangeState',
                result,
            })
            return result
        },
        // *deleted({ noticeId }, { call, put }) {
        //     const result = yield call(deletedNotice, { notice_id: noticeId })
        //     yield put({
        //         type: 'beforeChangeState',
        //         result,
        //     })
        //     return result
        // },

        *beforeChangeState({ result, field }, { put }) {
            if (result.status === 0) {
                if (result.data && field) {
                    yield put({
                        type: 'changeState',
                        result,
                        field
                    })
                }
                if (result.token) {
                    yield put({
                        type: 'token/changeState',
                        token: result.token,
                        ...result.data
                    })
                }
            }
        }
    },
    reducers: {
        changeState(state, { result, field }) {
            const nextState = JSON.parse(JSON.stringify({ data: state })).data
            nextState[field] = result.data
            return nextState
        }
    }
}